﻿# -*- coding: utf-8 -*-
"""
Created on Sun Jun 30 23:43:56 2019

@author: ASUS
"""

import os
from multiprocessing import Process
from scrapy import cmdline
from datetime import datetime
#import tkinter as tk
#from tkinter import  *
from xgb_odds.pred_from_odds import xgb_odds_predict
from lstm_series_odds.pred_from_series_odds import lstm_series_odds_predict


import warnings
warnings.filterwarnings("ignore")



def spider_get_match_data(other_leagues, time_range_left, time_range_right):
    print('球探网爬虫进程开始运行...')
    cmdline.execute('scrapy crawl dolores -a other_leagues={} -a time_range_left={} -a time_range_right={}'.format(other_leagues, time_range_left, time_range_right).split())
    # cmdline.execute('scrapy crawl elo'.split())



def delete_database(db):
    #1 先清除旧数据
    del_sql1 = 'truncate table all_match_score;'
    del_sql2 = 'truncate table all_match_oz_odds;'
    del_sql3 = 'truncate table all_match_az_odds;'
    del_sql4 = 'truncate table all_bs_data;'
    del_sql5 = 'truncate table taday_matchs;'
    del_sql6 = 'truncate table all_match_company_oz_odds'
    del_sql7 = 'truncate table today_match_time_series_oz_odds'
    # db.del_item(del_sql1)
    # db.del_item(del_sql2)
    # db.del_item(del_sql3)
    # db.del_item(del_sql4)
    # db.del_item(del_sql5)
    db.del_item(del_sql6)
    db.del_item(del_sql7)

#3 合并数据，保存到文件
def delete_datafile():
    print('导出今日比赛数据...')
    if (os.path.exists(r'./prediction/datasets/today_matchs_company_odds.csv')):
        os.remove(r'./prediction/datasets/today_matchs_company_odds.csv')

    if (os.path.exists(r'./prediction/datasets/today_matchs_time_series_odds.csv')):
        os.remove(r'./prediction/datasets/today_matchs_time_series_odds.csv')

    if (os.path.exists(r'./prediction/datasets/xbs_odds_predResult.csv')):
        os.remove(r'./prediction/datasets/xbs_odds_predResult.csv')

    if (os.path.exists(r'./prediction/datasets/lstm_odds_predResult_3_class.csv')):
        os.remove(r'./prediction/datasets/lstm_odds_predResult_3_class.csv')

'''
class FootballExpert():
    def __init__(self, master: tk.Tk):
        self.root = master
        self.root.title('红日')
        # 更改左上角窗口的的icon图标,加载logo标
        # self.root.iconbitmap(r'./Data/playfootball.ico')
        # 设置窗口大小:宽x高,注,此处不能为 "*",必须使用 "x"
        self.root.geometry('500x250')
        # 设置主窗口的背景颜色,颜色值可以是英文单词，或者颜色值的16进制数,除此之外还可以使用Tk内置的颜色常量
        self.root["background"] = "#C9C9C9"
        # 窗口不能被拉伸
        self.root.resizable(0, 0)

        self.time_left_var = DoubleVar()
        self.time_right_var = DoubleVar()
        self.is_download_var = IntVar()
        # other_leagues_var = StringVar()
        self.time_left_var.set(-0.25)
        self.time_right_var.set(12)

        time_label = tk.Label(self.root, text="时间范围(小时):", font=(15))
        separator_label = tk.Label(self.root, text=" ~ ", font=(15))
        time_entry_left = tk.Entry(self.root, textvariable=self.time_left_var, font=(15))
        time_entry_right = tk.Entry(self.root, textvariable=self.time_right_var, font=(15))
        # downlode_label = tk.Label(self.root, text="是否下载比赛数据:", font=(15))
        is_downlode_check = tk.Checkbutton(self.root, text="是否下载数据", font=(15), variable=self.is_download_var, onvalue=0, offvalue=1)
        other_leagues_label = tk.Label(self.root, text="其他联、杯名称:", font=(15))
        self.other_leagues_text = tk.Text(self.root, width=100, height=10, undo=True, autoseparators=False, font=(15))

        self.other_leagues_text.insert(INSERT, r"")

        time_label.place(x=5, y=5, width=150, height=20)
        time_entry_left.place(x=160, y=5, width=80, height=20)
        separator_label.place(x=240, y=5, width=20, height=20)
        time_entry_right.place(x=260, y=5, width=80, height=20)
        is_downlode_check.place(x=360, y=5, width=160, height=20)
        other_leagues_label.place(x=5, y=30, width=150, height=20)
        self.other_leagues_text.place(x=160, y=30, width=580, height=150)


        # 添加按钮，以及按钮的文本，并通过command 参数设置关闭窗口的功能
        predict_button = tk.Button(root, text="开始预测",font=(70),  command=self.predict_start)

        # 将按钮放置在主窗口内
        predict_button.pack(side="bottom")

    def predict_start(self):
        time_range_left = self.time_left_var.get()
        time_range_right = self.time_right_var.get()
        other_leagues1 = self.other_leagues_text.get('0.0','end')

        with open('leagues.txt', 'r', encoding="utf-8") as file:
            other_leagues2 = file.read()
            kk = 0

        # other_leagues2 = r"欧罗巴杯,欧冠杯,塞浦甲,意杯,德国杯,比利时杯,哥伦甲春,伊拉联,智利甲,印尼超,希腊杯,阿曼联,伊朗杯,墨西甲秋,墨西联秋,卡塔尔联,摩洛超,亚冠杯,法国杯,卡塔尔联,欧洲杯,非洲杯,日联杯,自由杯, 南球杯,巴西杯,哥伦甲春,墨西甲,塞尔超降,波兰杯,奥甲冠,捷甲冠,捷甲附,丹麦超冠,巴林超,哥斯甲秋,洪都甲秋,英足总杯,意超杯,土杯,意杯,印度超,西杯,荷兰杯,苏超冠,罗甲降,塞尔超冠,塞尔超降,墨西联附,比甲冠,丹麦超降,丹麦超冠,奥甲冠,捷甲降,克亚甲,拉脱超,乌兹超,突尼甲冠,埃及超,瑞典杯,爱超,欧冠杯,墨西联春,美金杯,阿甲,智利杯,越南联,印尼超,中超,缅甸联,新加坡联,缅甸联,韩K2联,哈萨超,拉脱超,秘鲁甲秋,巴西乙,玻利甲春,阿尔甲,日皇杯,欧冠杯,南球杯,美金杯,巴西杯,挪威杯,孟加拉联,马来超,保超,芬甲,爱超,巴西乙,墨西联春,哥伦甲秋,罗甲,阿根廷杯,女世界杯,俄杯,哥斯甲春,哥伦杯,欧会杯,洪都甲,乌克超,国际友谊,阿美超"
        other_leagues = other_leagues2 + "," + other_leagues1

        # other_leagues = r"欧罗巴杯, 欧冠杯, 塞浦甲, 意杯, 德国杯,比利时杯,哥伦甲春,伊拉联,智利甲,印尼超,希腊杯,阿曼联,伊朗杯,墨西甲秋,墨西联秋,卡塔尔联,摩洛超,亚冠杯,法国杯,卡塔尔联,欧洲杯,非洲杯,日联杯,自由杯, 南球杯,巴西杯,哥伦甲春,墨西甲,塞尔超降,波兰杯,奥甲冠,捷甲冠,捷甲附,丹麦超冠,巴林超,哥斯甲秋,洪都甲秋,英足总杯,意超杯,土杯,意杯,英联杯,印度超,西杯,荷兰杯,苏超冠,罗甲降,塞尔超冠,塞尔超降"
        other_leagues = ''.join([i.strip(' ') for i in other_leagues])

        match_is_download = True
        # match_is_download = False

        if match_is_download:
            ##1 先清除旧数据
            delete_datafile()
            ##2 开启爬虫进程
            print('%',os.getpid())#主进程号
            spi = Process(target=spider_get_match_data, args=(other_leagues, time_range_left, time_range_right))#创建子进程对象，并传参args=(X,),注意只传一个参数的时候必须在参数后加逗号
            spi.start()
            spi.join()
            print('球探网爬虫进程结束...')
            print('%', os.getppid())#主进程的父进程


        #4 预测
        ### 方法1：dolores算法
        # predict_match(db)
        # # ### 方法2：xgboost,pi特征算法
        # weekpred()
        ### 方法3：xgboost,菠菜公司赔率算法
        xgb_odds_predict()

        # 方法4：lstm,菠菜公司时序赔率算法
        lstm_series_odds_predict()
        print('完成预测')
'''


# def start_up():
if __name__ == "__main__":
    # import sys
    # print(sys.path)
    ## 限制软件的使用日期
    # if datetime.today() > datetime(2023, 10, 1):
    #     print("####### 软件已过期 #######")
    #     exit(0)
    '''
    root = tk.Tk()
    mainpage = FootballExpert(master=root)
    root.mainloop()
    '''
    with open('leagues.txt', 'r', encoding="utf-8") as file:
        other_leagues = file.read()
        kk = 0
    # other_leagues = r"欧罗巴杯, 欧冠杯, 塞浦甲 , 意杯, 德国杯,比利时杯,哥伦甲春,伊拉联,智利甲,印尼超,希腊杯,阿曼联,伊朗杯,墨西甲秋,墨西联秋,卡塔尔联,摩洛超,亚冠杯,法国杯,卡塔尔联,欧洲杯,非洲杯,日联杯,自由杯, 南球杯,巴西杯,哥伦甲春,墨西甲,塞尔超降,波兰杯,奥甲冠,捷甲冠,捷甲附,丹麦超冠,巴林超,哥斯甲秋,洪都甲秋,英足总杯,意超杯,土杯,意杯,英联杯,印度超,西杯,荷兰杯,苏超冠,罗甲降,塞尔超冠,塞尔超降"
    other_leagues = ''.join([i.strip(' ') for i in other_leagues])
    time_range_left = -0.5
    time_range_right = 12


    match_is_download = True
    # match_is_download = False

    if match_is_download:
        ##1 先清除旧数据
        delete_datafile()
        ##2 开启爬虫进程
        print('%',os.getpid())#主进程号
        spi = Process(target=spider_get_match_data, args=(other_leagues, time_range_left, time_range_right))#创建子进程对象，并传参args=(X,),注意只传一个参数的时候必须在参数后加逗号
        spi.start()
        spi.join()
        print('球探网爬虫进程结束...')
        print('%', os.getppid())#主进程的父进程


    #4 预测
    ### 方法1：dolores算法
    # predict_match(db)
    # # ### 方法2：xgboost,pi特征算法
    # weekpred()
    ### 方法3：xgboost,菠菜公司赔率算法
    xgb_odds_predict()

    # 方法4：lstm,菠菜公司时序赔率算法
    lstm_series_odds_predict()
    print('完成预测')




